﻿/**********************************************************
 ●项目：Smartkernel
 ●公司：智能核心信息技术研发中心
 ●作者：曹艳白
 ●邮箱：caoyanbai@gmail.com
 ●创建：2008-08-10
**********************************************************/
using System;
using System.IO;
using System.Security.Cryptography;
using System.Web.Security;

namespace Smartkernel.Security.Cryptography
{
    /// <summary>
    /// Md5操作
    /// </summary>
    public static class SmartHashPassword
    {
        /// <summary>
        /// 获得哈希密码
        /// </summary>
        /// <param name="input">待处理的文本</param>
        /// <returns>哈希密码</returns>
        public static string HashPassword(string input)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(input, "md5");
        }
        /// <summary>
        /// 计算文件的希密码
        /// </summary>
        /// <param name="path">文件的路径</param>
        /// <returns>编码了的哈希码</returns>
        public static string FileHashPassword(string path)
        {
            using (HashAlgorithm hashAlgorithm = HashAlgorithm.Create("SHA1"))
            {
                using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
                {
                    byte[] hash = hashAlgorithm.ComputeHash(stream);
                    return SmartBase64.Encode(BitConverter.ToString(hash));
                }
            }
        }
        /// <summary>
        /// 计算流的希密码
        /// </summary>
        /// <param name="stream">流</param>
        /// <returns>编码了的哈希码</returns>
        public static string FileHashPassword(Stream stream)
        {
            using (HashAlgorithm hashAlgorithm = HashAlgorithm.Create("SHA1"))
            {
                byte[] hash = hashAlgorithm.ComputeHash(stream);
                return SmartBase64.Encode(BitConverter.ToString(hash));
            }
        }
    }
}
